package com.project.springboot.enumparam;

import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import java.util.Map;

/**
 * description: 枚举转换器工厂
 * @author 朱正磊
 * @date 2024-01-18 14:14:20
 */
public class IdCodeToEnumConverterFactory implements ConverterFactory<String, IdCodeBaseEnum> {

    @SuppressWarnings("rawtypes")
    private static final Map<Class, Converter> CONVERTERS = Maps.newHashMap();

    /**
     * description: 获取枚举转换器
     * @author 朱正磊
     * @date 2024-01-18 14:15:16
     * @param targetType 枚举字节码对象
     * @return 返回枚举转换器
     * @param <T> 枚举泛型
     */
    @NotNull
    @Override
    public <T extends IdCodeBaseEnum> Converter<String, T> getConverter(@NotNull Class<T> targetType) {
        // noinspection unchecked
        Converter<String, T> converter = CONVERTERS.get(targetType);
        if (converter == null) {
            converter = new IdCodeToEnumConverter<>(targetType);
            CONVERTERS.put(targetType, converter);
        }
        return converter;
    }

}
